package comment

import (
	"common/models"
	"common/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"strconv"
	"strings"
)

// DeleteComment 删除评论
// @Summary 删除评论
// @Description 删除评论
// @Tags 评论
// @Accept application/json
// @Produce application/json
// @Param Authorization header string true "Bearer 用户令牌"
// @Param object body docModel.DocDeleteCommentReq false "参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /comment/del [post]
func (h CHandler) DeleteComment(c *gin.Context) {
	midList := make([]string, 0)
	resultList := make([]string, 0)
	mid := &midList
	result := &resultList
	i := 0
	resp := new(models.RespData)
	req := new(models.DeleteCommentReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Comment:DeleteComment with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Comment:DeleteComment with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//获得当前评论
	commentInfo, err := h.M.CommentInfoQueryByUid(req.CommentId)
	if err != nil {
		zap.L().Error("Comment:DeleteComment h.M.CommentInfoQueryByUid err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	//判断当前节点是否有父节点
	if commentInfo.ParentId != models.SplitFormat {
		//查找父节点的数据
		PcommentInfo, err := h.M.CommentInfoQueryById(commentInfo.ParentId)
		if err != nil {
			zap.L().Error("Comment:DeleteComment h.M.CommentInfoQueryById err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		child := strings.Split(PcommentInfo.ChildIds, models.SplitFormat)
		//删除元素
		DeleteValList(strconv.Itoa(int(commentInfo.ID)), &child)
		nowChildList := make([]string, 0)
		nowChildStr := ""
		for _, v := range child {
			if v != "" {
				nowChildList = append(nowChildList, v)
			}
		}
		if len(nowChildList) != 0 {
			nowChildStr = strings.Join(nowChildList, models.SplitFormat)
		}
		PcommentInfo.ChildIds = nowChildStr
		//更新父节点
		err = h.M.CommentInfoSaveById(PcommentInfo)
		if err != nil {
			zap.L().Error("Comment:DeleteComment h.M.CommentInfoSaveById err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
	}
	*result = append(*result, strconv.Itoa(int(commentInfo.ID)))
	childList := strings.Split(commentInfo.ChildIds, models.SplitFormat)
	//判断是否有子节点
	for _, v := range childList {
		if v != "" {
			*mid = append(*mid, v)
			i++
		}
	}
	if i > 0 {
		h.getCommentIds(mid, result)
	}
	//遍历删除
	for _, v := range *result {
		err := h.M.CommentInfoDelById(v)
		if err != nil {
			zap.L().Error("Comment:DeleteComment h.M.CommentInfoQueryByUid err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
	}
	resp.SendSuccess(c)
}
func (h CHandler) getCommentIds(mid, result *[]string) {
	if len(*mid) == 0 {
		return
	}
	i := 0
	childList := make([]string, 0)
	commentId := ChangeList(mid)
	info, err := h.M.CommentInfoQueryById(commentId)
	if err != nil {
		return
	}
	//将本次查找的节点放到result中
	*result = append(*result, strconv.Itoa(int(info.ID)))
	childList = strings.Split(info.ChildIds, models.SplitFormat)
	for _, v := range childList {
		if v != "" {
			i++
			childList = append(childList, v)
		}
	}
	if len(childList) == 0 {
		return
	}
	//将子节点放到mid中
	*mid = append(*mid, childList...)
	h.getCommentIds(mid, result)
}
func ChangeList(nums *[]string) (re string) {
	if len(*nums) == 0 {
		return
	}
	re = (*nums)[0]
	*nums = (*nums)[1:]
	return
}
func DeleteValList(val string, nums *[]string) {
	if len(*nums) == 0 {
		return
	}
	index := -1
	for i, v := range *nums {
		if v == val {
			index = i
			break
		}
	}
	copy((*nums)[index:], (*nums)[index+1:])
	//删除最后一个位置
	(*nums)[len(*nums)-1] = ""
}
